package org.company.management.ui;

import org.company.management.controller.EmployeeGroupJpaController;
import org.company.management.controller.StatusLineController;
import org.company.management.controller.UiController;
import org.company.management.entity.EmployeeGroup;
import org.company.management.utilities.CustomValidatedText;
import org.company.management.utilities.EurosPerHourValidator;
import org.company.management.utilities.NameValidator;
import org.company.management.utilities.Strings;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.wb.swt.layout.grouplayout.GroupLayout;
import org.eclipse.wb.swt.layout.grouplayout.LayoutStyle;

/**
 * @author Paner
 *
 */
public class AddGroupEditor extends EditorPart implements SelectionListener{

	public final static String ID = Strings.addGroupEditorId;
	
	private Group _employeesgroupGroup;
	private Button _backButton;
	private Button _addButton;
	private CustomValidatedText _nameText;
	private CustomValidatedText _hourlypaymentText;
	
	/* 
	 * Constructor, sets title image for editor
	 */
	public AddGroupEditor() {
		setTitleImage(ResourceManager.getPluginImage(Strings.pluginId, Strings.addUserPicture16x16));
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);

	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		createControls(parent);
		createLayout(parent);
		
	}
	
	/*
	 * Creates widgets
	 */
	private void createControls(Composite parent)
	{
		_employeesgroupGroup = new Group(parent, SWT.NONE);
		_employeesgroupGroup.setText(Strings.newGroup);
		
		_backButton = new Button(parent, SWT.NONE);
		_backButton.setToolTipText(Strings.backTooltip);
		_backButton.setImage(ResourceManager.getPluginImage(Strings.pluginId, Strings.backButtonPicture32x32));
		_backButton.addSelectionListener(this);
		
		_addButton = new Button(parent, SWT.NONE);
		_addButton.setToolTipText(Strings.confirmTooltip);
		_addButton.setImage(ResourceManager.getPluginImage(Strings.pluginId, Strings.confirmButtonPicture32x32));
		_addButton.addSelectionListener(this);
				
		_nameText = new CustomValidatedText(_employeesgroupGroup, new NameValidator());
		_nameText.setBounds(114, 63, 239, 21);

		_hourlypaymentText = new CustomValidatedText(_employeesgroupGroup, new EurosPerHourValidator());
		_hourlypaymentText.setBounds(114, 116, 239, 21);

		
		Label groupnameLabel = new Label(_employeesgroupGroup, SWT.NONE);
		groupnameLabel.setText(Strings.group);
		groupnameLabel.setBounds(29, 66, 55, 15);
		
		Label hourlypaymentLabel = new Label(_employeesgroupGroup, SWT.NONE);
		hourlypaymentLabel.setText(Strings.euroPerHour);
		hourlypaymentLabel.setBounds(29, 119, 70, 15);

	}
	
	/*
	 * Creates layout
	 */
	private void createLayout(Composite parent)
	{
		GroupLayout gl_parent = new GroupLayout(parent);
		gl_parent.setHorizontalGroup(
			gl_parent.createParallelGroup(GroupLayout.LEADING)
				.add(gl_parent.createSequentialGroup()
					.addContainerGap()
					.add(_backButton)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
				.add(GroupLayout.TRAILING, gl_parent.createSequentialGroup()
					.addContainerGap(523, Short.MAX_VALUE)
					.add(_addButton)
					.addContainerGap())
				.add(gl_parent.createSequentialGroup()
					.add(92)
					.add(_employeesgroupGroup, GroupLayout.PREFERRED_SIZE, 417, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(85, Short.MAX_VALUE))
		);
		gl_parent.setVerticalGroup(
			gl_parent.createParallelGroup(GroupLayout.LEADING)
				.add(gl_parent.createSequentialGroup()
					.addContainerGap()
					.add(_backButton)
					.add(60)
					.add(_employeesgroupGroup, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.RELATED, 43, Short.MAX_VALUE)
					.add(_addButton)
					.addContainerGap())
		);
		parent.setLayout(gl_parent);
	}
	
	/*
	 * Checks that required fields are filled
	 */
	private boolean validateUserInput()
	{
		boolean proceed = false;
		if(_nameText.isValid() && _hourlypaymentText.isValid() )
		{
			proceed = true;
		}
		
		return proceed;
		
	}

	@Override
	public void widgetSelected(SelectionEvent e)
	{
		Object selectedWidget = e.getSource();
		if(selectedWidget == _addButton)
		{
			try
			{
				if(validateUserInput())
				{
					//Create and fill a new group for user given values
					EmployeeGroup employeeGroup = new EmployeeGroup();
					employeeGroup.setName(_nameText.getText());
					employeeGroup.setHourlyrateofpayment(Float.parseFloat(_hourlypaymentText.getText()));
					
					EmployeeGroupJpaController employeeGroupDao = new EmployeeGroupJpaController();
					employeeGroupDao.create(employeeGroup);
					
					StatusLineController.setStatusLine(getEditorSite(), Strings.validInputMessage);
				}
				else
				{
					StatusLineController.setStatusLine(getEditorSite(), Strings.invalidInputMessage);
				}
			}
			catch(Exception ex)
			{
				//throw;
			}
		}
		else if (selectedWidget == _backButton)
		{
			UiController.showEditor(Strings.newGroup, GroupEditor.ID);
		}
		
	}
	
	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
